//
//  NSMutableArray+Addition.m
//  Project
//
//  Created by  CarlsonLee on 2018/12/7.
//  Copyright © 2018年 YANJING. All rights reserved.
//

#import "NSMutableArray+Addition.h"

@implementation NSMutableArray (Addition)

+ (void)load {
    [super load];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [objc_getClass("__NSArray0") gel_systemSelector:@selector(objectAtIndex:) swizzledSelector:@selector(gel_objectAtIndex0:) error:nil];
        [objc_getClass("__NSArrayI") gel_systemSelector:@selector(objectAtIndex:) swizzledSelector:@selector(gel_objectAtIndex:) error:nil];
        [objc_getClass("__NSSingleObjectArrayI") gel_systemSelector:@selector(objectAtIndex:) swizzledSelector:@selector(gel_objectAtIndexS:) error:nil];
        [objc_getClass("__NSArrayI") gel_systemSelector:@selector(objectAtIndexedSubscript:) swizzledSelector:@selector(gel_objectAtIndexedSubscript:) error:nil];
    });
}

- (id)gel_objectAtIndex:(NSUInteger)index {
    if(index<self.count){
        return [self gel_objectAtIndex:index];
    }else{
        NSLog(@"数组已越界objectAtIndex = %ld  count = %ld", index, self.count);
        return nil;
    }
}

- (id)gel_objectAtIndex0:(NSUInteger)index {
    if(index<self.count){
        return [self gel_objectAtIndex0:index];
    }else{
        NSLog(@"数组已越界objectAtIndex = %ld  count = %ld", index, self.count);
        return nil;
    }
}

- (id)gel_objectAtIndexS:(NSUInteger)index {
    if(index<self.count){
        return [self gel_objectAtIndexS:index];
    }else{
        NSLog(@"数组已越界objectAtIndex = %ld  count = %ld", index, self.count);
        return nil;
    }
}

- (id)gel_objectAtIndexedSubscript:(NSUInteger)index {
    if(index<self.count){
        return [self gel_objectAtIndex:index];
    }else{
        NSLog(@"数组已越界objectAtIndex = %ld  count = %ld", index, self.count);
        return nil;
    }
}

- (void)gel_reverse {
    NSUInteger count = self.count;
    int mid = floor(count / 2.0);
    for (NSUInteger i = 0; i < mid; i++) {
        [self exchangeObjectAtIndex:i withObjectAtIndex:(count - (i + 1))];
    }
}

- (void)gel_shuffle {
    for (NSUInteger i = self.count; i > 1; i--) {
        [self exchangeObjectAtIndex:(i - 1)
                  withObjectAtIndex:arc4random_uniform((u_int32_t)i)];
    }
}

- (void)gel_appendObject:(id)anObject {
    if (anObject && ![anObject isKindOfClass:[NSNull class]]) {
        [self addObject:anObject];
    }
}

- (void)gel_appendObjects:(NSArray *)objects {
    if (objects && [objects isKindOfClass:[NSArray class]]) {
        [self addObjectsFromArray:objects];
    }
}

- (void)gel_prependObject:(id)anObject {
    [self insertObject:anObject atIndex:0];
}

- (void)gel_prependObjects:(NSArray *)objects {
    if (!objects) return;
    NSUInteger i = 0;
    for (id obj in objects) {
        [self insertObject:obj atIndex:i++];
    }
}

- (void)gel_insertObjects:(NSArray *)objects atIndex:(NSUInteger)index {
    NSUInteger i = index;
    for (id obj in objects) {
        [self insertObject:obj atIndex:i++];
    }
}

@end
